<?php

/**
 * 后台首页控制器
 * @author:Cloud <190296465@qq.com>
 * @copyright:TcitCMS
 * @since:2014-05-12
 */

namespace Admin\Controller;

class CoreController extends AdminController {

    function index() {
        $model = new \Common\Model\MenuModel();
        $map['id'] = array('GT', 10);
        $this->map = $map;
        $this->order = 'sort_no asc';
        $this->pagesize = C('DEFAULT_PAGE_SIZE_10');
        $this->url = U(CONTROLLER_NAME . '/index');
        parent::commonIndex($model);
    }

    function add() {
        $model = new \Common\Model\MenuModel();
        parent::commonAdd($model);
    }

    function save() {
        $model = new \Common\Model\MenuModel();
        parent::commonSave($model);
    }

    function delete() {
        $model = new \Common\Model\MenuModel();
        parent::commonDelete($model);
    }

    function system() {
        $info = array(
            'safe_mode' => array('运行于安全模式', 0),
            'allow_url_fopen' => array('访问 URL 对象', 'OFF', '建议关闭'),
            'register_globals' => array('注册全局变量', 'OFF', '建议关闭'),
            'magic_quotes_gpc' => array('魔术引号开启', 'OFF', '建议关闭'),
            'short_open_tag' => array('短标记支持', 'OFF', '建议关闭'),
            'magic_quotes_runtime' => array('自动转义溢出字符', 'OFF', '建议关闭'),
            'enable_dl' => array('允许动态加载链接库', 'OFF', '建议打开'),
            'display_errors' => array('显示错误信息', 0),
            'post_max_size' => array('post最大数据量', 0),
            'upload_max_filesize' => array('上传文件的最大大小', 0),
            'memory_limit' => array('脚本最大内存使用量', 1),
        );
        $fun = array(
            'mysql_close' => array('MySQL数据库', 'ON', '推荐使用MySQL数据库'),
            'gd_info' => array('图形处理 GD 库', 'ON', '建议打开GD库'),
            'socket_accept' => array('Socket支持', 0),
            'xml_set_object' => array('XML解析支持', 'ON', '建议开启'),
            'gzclose' => array('压缩文件支持(Zlib)', 0),
            'mcrypt_cbc' => array('MCrypt加密处理', 0),
            'preg_match' => array('PREL相容语法 PCRE', 'ON', '必须打开PREL语法兼容'),
        );
        $phpversiON = phpversion(); //获取php版本号
        $versiON_msg = versiON_compare(PHP_VERSION, '5.2.0', '<') ? '推荐使用5.2.0以上版本' : '正常使用';
        $run_env = strtoupper(php_sapi_name()); //查看运行环境
        $os = PHP_OS;
        $is_rewrite = false; //预设rewrite为否定
        if (($is_apache = strpos($run_env, 'APACHE')) !== false) {
            $apache_list = apache_get_modules();
            $is_rewrite = array_search('mod_rewrite', $apache_list) ? true : false;
            unset($apache_list);
        }

        foreach ($info as $key => &$val) {
            $var = get_cfg_var($key) ? get_cfg_var($key) : ini_get($key);
            $val['value'] = ($var == 0) ? 'OFF' : (($var == 1) ? 'ON' : $var);
            if (is_numeric($val[1]) || $val['value'] == $val[1]) {
                $val['message'] = '<span class="blue">√</span>';
            } else {
                $val['message'] = '<span class="red">' . $val[2] . '</span>';
            }
        }
        foreach ($fun as $key => &$val) {
            $val['value'] = functiON_exists($key) ? 'ON' : 'OFF';
            if (is_numeric($val[1]) || $val['value'] == $val[1]) {
                $val['message'] = '<span class="blue">√</span>';
            } else {
                $val['message'] = '<span class="red">' . $val[2] . '</span>';
            }
        }

        $moban = '<div class="layui-fluid pt-10 pr-10 pl-20 pb-20">';
        $moban .= '<fieldset class="layui-elem-field layui-field-title"><legend>服务器基本参数</legend></fieldset>';
        $moban .= '<table class="layui-table">';
        $moban .= '<colgroup>';
        $moban .= '<col width="400">';
        $moban .= '<col>';
        $moban .= '</colgroup>';
        $moban .= '<thead>';
        $moban .= '<tr>';
        $moban .= '<th>参数名称</th>';
        $moban .= '<th>值</th>';
        $moban .= '</tr>';
        $moban .= '</thead>';
        $moban .= '<tbody>';
        $moban .= '<tr>';
        $moban .= '<td>服务器域名/IP：</td>';
        $moban .= '<td>' . $_SERVER['SERVER_NAME'] . '<' . $_SERVER['REMOTE_ADDR'] . '></td>';
        $moban .= '</tr>';
        $moban .= '<tr>';
        $moban .= '<td>服务端口：</td>';
        $moban .= '<td>' . $_SERVER['SERVER_PORT'] . '</td>';
        $moban .= '</tr>';
        $moban .= '<tr>';
        $moban .= '<td>服务器类型/版本：</td>';
        $moban .= '<td>' . $_SERVER['SERVER_SOFTWARE'] . '</td>';
        $moban .= '</tr>';
        $moban .= '<tr>';
        $moban .= '<td>服务器操作系统：</td>';
        $moban .= '<td>' . $os . '</td>';
        $moban .= '</tr>';
        $moban .= '<tr>';
        $moban .= '<td>网站根目录：</td>';
        $moban .= '<td>' . $_SERVER['DOCUMENT_ROOT'] . '</td>';
        $moban .= '</tr>';
        $moban .= '<tr>';
        $moban .= '<td>当前文件所在位置：</td>';
        $moban .= '<td>' . $_SERVER['SCRIPT_FILENAME'] . '</td>';
        $moban .= '</tr>';
        $moban .= '</tbody>';
        $moban .= '</table>';

        $moban .= '<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;"><legend>服务器变量检测</legend></fieldset>';
        $moban .= '<table class="layui-table">';
        $moban .= '<colgroup>';
        $moban .= '<col width="400">';
        $moban .= '<col width="300">';
        $moban .= '<col>';
        $moban .= '</colgroup>';
        $moban .= '<thead>';
        $moban .= '<tr>';
        $moban .= '<th>变量名称</th>';
        $moban .= '<th>当前状态</th>';
        $moban .= '<th>推荐</th>';
        $moban .= '</tr>';
        $moban .= '</thead>';
        $moban .= '<tbody>';
        $moban .= '<tr>';
        $moban .= '<td>PHP版本</td>';
        $moban .= '<td>' . $phpversiON . '</td>';
        $moban .= '<td>' . $versiON_msg . '</td>';
        $moban .= '</tr>';
        $moban .= '<tr>';
        $moban .= '<td>PHP运行环境</td>';
        $moban .= '<td>' . $run_env . '</td>';
        $moban .= '<td></td>';
        $moban .= '</tr>';

        $moban .= '<tr>';
        $moban .= '<td>是否开启rewrite</td>';
        if ($is_apache === false)
            $moban .= '<td>无法检测</td>';
        else
            $is_rewrite ? $moban .= '<td><span color="green">开启</span></td>' : $moban .= '<td><span class="red">未开启</span></td>';
        $moban .= '<td></td>';
        $moban .= '</tr>';
        foreach ($info as $key => $val) {
            $moban .= '<tr>';
            $moban .= '<td>' . $val[0] . '[' . $key . ']</td>';
            $moban .= '<td>' . $val['value'] . '</td>';
            $moban .= '<td>' . $val['message'] . '</td>';
            $moban .= '</tr>';
        }
        foreach ($fun as $key => $val) {
            $moban .= '<tr>';
            $moban .= '<td>' . $val[0] . '[' . $key . ']</td>';
            $moban .= '<td>' . $val['value'] . '</td>';
            $moban .= '<td>' . $val['message'] . '</td>';
            $moban .= '</tr>';
        }
        $moban .= '</tbody>';
        $moban .= '</table>';
        $moban .= '</div>';

        $this->assign('moban', $moban);
        $this->display();
    }

}

?>